/*
Copyright 2015 shizhefei（LuckyJayce）

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */
package com.pkutech.carcasher.content;

import android.text.TextUtils;
import android.util.Log;

import com.pkutech.carcasher.bean.HmSelectFormalOrderResponse;
import com.pkutech.carcasher.bean.SelectFormalOrderResponse;
import com.pkutech.carcasher.db.RealmManager;
import com.pkutech.carcasher.model.HmSelectFormalOrder;
import com.pkutech.carcasher.model.SelectFormalOrder;
import com.pkutech.carcasher.network.SoapApi;
import com.pkutech.carcasher.utils.CommonUtils;
import com.shizhefei.mvc.IDataCacheLoader;
import com.shizhefei.mvc.IDataSource;

import java.util.ArrayList;
import java.util.List;

import rx.Subscriber;

public class HmSelectFormalOrderDataSource implements IDataSource<List<HmSelectFormalOrder>>, IDataCacheLoader<List<HmSelectFormalOrder>> {
	private int page = 1;
	private int maxPage = 10;
	private int flag=0;//0 停靠模块  1 缴费模块
	List<HmSelectFormalOrder> netSelectFormalOrders = new ArrayList<HmSelectFormalOrder>();

	public String date;

	public HmSelectFormalOrderDataSource() {
	}

	public HmSelectFormalOrderDataSource(int flag) {
		this.flag = flag;
	}

	/**
	 * 加载缓存<br>
	 * 注意这个方法执行于UI线程，不要做太过耗时的操作<br>
	 * 每次刷新的时候触发该方法，该方法在DataSource refresh之前执行<br>
	 * 
	 * @param isEmpty
	 *            adapter是否有数据，这个值是adapter.isEmpty()决定
	 * @return 加载的数据，返回后会执行adapter.notifyDataChanged(data, true)<br>
	 *         相当于refresh执行后adapter.notifyDataChanged(data, true)
	 */
	@Override
	public List<HmSelectFormalOrder> loadCache(boolean isEmpty) {
//		if (isEmpty) {
//			List<HmSelectFormalOrder> selectFormalOrders = RealmManager.findHmAllSelectFormalOrders();
//			return selectFormalOrders;
//		}
		return null;
	}

	@Override
	public List<HmSelectFormalOrder> refresh() throws Exception {
		return loadData(1);
	}

	@Override
	public List<HmSelectFormalOrder> loadMore() throws Exception {
		return loadData(page + 1);
	}

	private List<HmSelectFormalOrder> loadData(int page) throws Exception {
		// 如果网路出错的话，直接抛异常不会执行后面的获取的语句
		//TODO
		SoapApi.hmSelectFormalOrder(RealmManager.findUserInfo().getCode(), date)
				.subscribe(new Subscriber<HmSelectFormalOrderResponse>() {
					@Override
					public void onCompleted() {

					}

					@Override
					public void onError(Throwable e) {
						netSelectFormalOrders.clear();
						RealmManager.clearhMSelectFormalOrders();
					}

					/**
					 1.判断这个是停靠模块还是缴费模块，因为两个用的接口是同一个。
					 2.如果是停靠模块，那就要显示所有的未补录的和补录的单子。
					 3.如果是缴费模块，就要过滤掉没有补录的单子。只有补录了的才能缴费。
					 * @param selectFormalOrderResponse
					 */
					@Override
					public void onNext(HmSelectFormalOrderResponse selectFormalOrderResponse) {
						netSelectFormalOrders.clear();
						RealmManager.clearhMSelectFormalOrders();
						if(selectFormalOrderResponse != null && !TextUtils.isEmpty(selectFormalOrderResponse.getStatus()) && "true".equals(selectFormalOrderResponse.getStatus())){
							List<HmSelectFormalOrder> formalOrders = selectFormalOrderResponse.getData();
							if(formalOrders != null && formalOrders.size() > 0){
								if(flag==0){
									netSelectFormalOrders = selectFormalOrderResponse.getData();
								}else {
									for(HmSelectFormalOrder formalOrder : formalOrders){
										if(!TextUtils.isEmpty(formalOrder.getCarnum()) && "1".equals(formalOrder.getCollectionstate())){
											netSelectFormalOrders.add(formalOrder);
										}
									}
								}

								maxPage = CommonUtils.calAllPageCount(netSelectFormalOrders.size());
								RealmManager.clearhMSelectFormalOrders();
								RealmManager.saveHmSelectFormalOrders(netSelectFormalOrders);
							}
						}
					}
				});
		Log.d("cyy", "maxPage:"+maxPage);
		this.page = page;
		return netSelectFormalOrders;
	}

	@Override
	public boolean hasMore() {
		return page < maxPage;
	}

}
